package com.module.prms.dao;



import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;
import base.util.Tools;

import com.module.prms.vo.PrmsOperation;




public class OperationDao extends GenericHibernateDao<PrmsOperation, String> {

	private OperationDao(){}
	
	private static OperationDao dao;
	
	public synchronized static OperationDao getInstance(){
		if(dao == null )
			dao = new OperationDao();
		return dao;
	}
	
	/**
	 * 操作列表
	 * huhui 2011-07-30
	 * @param bean  用户实体bean
	 * @param start 数据起始值
	 * @param limit 查询数量
	 * @return Page
	 */
	
    public Page getList(Session session,PrmsOperation bean,Integer start, Integer limit){
    	Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		StringBuffer sb = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		if (Tools.isValid(bean.getCmenuId())) {
			condition.append(" and obj.cmenuId = ?");
			params.add(bean.getCmenuId());
		}
		sb.append("from PrmsOperation as obj where 1=1 ");
		sb.append(condition);
		Query query = session.createQuery(sb.toString());
		int i = 0;
		for (Object obj : params) {
			query.setParameter(i, obj);
			i++;
		}
		query.setFirstResult(start).setMaxResults(limit);
		Query totalQuery = session
				.createQuery("select count(*) from PrmsOperation as obj where 1=1 "
						+ condition);
		int j = 0;
		for (Object obj : params) {
			totalQuery.setParameter(j, obj);
			j++;
		}
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
		page.setRows(query.list());
		return page;
    }
}
